import path from 'node:path';
import fs from 'node:fs';

const dirname = path.dirname(import.meta.url).replace('file:///', '')

fs.readdirSync(dirname).forEach(filename => {
    if (!filename.endsWith('svg') || !filename.includes('24dp')) return;
    const oldFileName = path.join(dirname, filename);
    const [iconName, config] = filename.split('_24dp_');
    const newFileName = path.join(dirname, `${iconName}${config.includes('FILL1') ? '_fill' : ''}.svg`);
    fs.renameSync(oldFileName, newFileName);
});

fs.readdirSync(dirname).forEach((filename) => {
    if (!filename.endsWith('svg')) return;
    const fullPath = path.join(dirname, filename);
    const svg = fs.readFileSync(fullPath).toString();
    const jsFullPath = fullPath.replace('.svg', '.js');
    const iconName = filename.replace('.svg', '').replace(/^\w|_\w/g, $1 => $1.replace('_', '').toUpperCase())

    const svgText = svg.replace('height="24px"', 'height="${size}px"')
        .replace('width="24px"', 'width="${size}px"')
        .replace('fill="#e8eaed"', 'fill="${color}"')

    fs.writeFileSync(jsFullPath, `export function Icon${iconName}({color = '#ffffff', size = 24} = {}) {
    return \`${svgText}\`
}`)
}, []);


const icons = fs.readdirSync(dirname).reduce((cache, filename) => {
    if (!filename.endsWith('.js') || filename.includes('index.js') || filename.includes('rename.mjs')) return cache;
    const iconName = filename.replace('.js', '').replace(/^\w|_\w/g, $1 => $1.replace('_', '').toUpperCase())
    cache.push(`export {Icon${iconName}} from './${filename}';`)
    return cache;
}, []);

const indexFilePath = path.join(dirname, 'index.js');

fs.writeFileSync(indexFilePath, icons.join('\n'));